package site.potatoblog.threadpool.manager.builder.impl;

import site.potatoblog.threadpool.manager.builder.Builder;
import site.potatoblog.threadpool.manager.core.DynamicThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

public class DynamicThreadPoolBuilder implements Builder<DynamicThreadPool> {
    private String appName;
    private int corePoolSize;
    private int maxPoolSize;
    private int keepAliveSeconds;
    private int queueCapacity;
    private ThreadFactory threadFactory;
    private RejectedExecutionHandler handler;
    private boolean allowCoreThreadTimeOut;
    private String alerEmail;
    private double capacityThreshold;
    private double activeThreshold;


    private DynamicThreadPoolBuilder() {
        appName="defaultApp";
        corePoolSize=1;
        maxPoolSize=Integer.MAX_VALUE;
        keepAliveSeconds=60;
        queueCapacity=Integer.MAX_VALUE;
        threadFactory= Executors.defaultThreadFactory();
        handler=new ThreadPoolExecutor.CallerRunsPolicy();
        allowCoreThreadTimeOut=false;
        capacityThreshold=0.8;
        activeThreshold=0.8;
        alerEmail="2974185325@qq.com";
    }

    public DynamicThreadPoolBuilder setAppName(String appName) {
        this.appName = appName;
        return this;
    }

    public DynamicThreadPoolBuilder setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
        return this;
    }

    public DynamicThreadPoolBuilder setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
        return this;
    }

    public DynamicThreadPoolBuilder setKeepAliveSeconds(int keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
        return this;
    }

    public DynamicThreadPoolBuilder setQueueCapacity(int queueCapacity) {
        this.queueCapacity = queueCapacity;
        return this;
    }

    public DynamicThreadPoolBuilder setThreadFactory(ThreadFactory threadFactory) {
        if(threadFactory!=null){
            this.threadFactory = threadFactory;
        }
        return this;
    }

    public DynamicThreadPoolBuilder setHandler(RejectedExecutionHandler handler) {
        if(handler!=null){
            this.handler = handler;
        }
        return this;
    }

    public DynamicThreadPoolBuilder setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
        this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
        return this;
    }

    public DynamicThreadPoolBuilder setAlerEmail(String alerEmail) {
        this.alerEmail = alerEmail;
        return this;
    }

    public DynamicThreadPoolBuilder setCapacityThreshold(double capacityThreshold) {
        this.capacityThreshold = capacityThreshold;
        return this;
    }
    public static DynamicThreadPoolBuilder create(){
        return new DynamicThreadPoolBuilder();
    }

    public void setActiveThreshold(double activeThreshold) {
        this.activeThreshold = activeThreshold;
    }

    @Override
    public DynamicThreadPool build() {
        DynamicThreadPool dynamicThreadPool=new DynamicThreadPool(
                appName,
                corePoolSize,
                maxPoolSize,
                keepAliveSeconds,
                queueCapacity,
                threadFactory,
                handler,
                capacityThreshold,
                activeThreshold,
                alerEmail
        );
        dynamicThreadPool.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
        return dynamicThreadPool;
    }


}
